package com.dk.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dk.mapper.PersonRoleMapper;
import com.dk.mapper.TPersonMapper;
import com.dk.pojo.PersonRoleExample;
import com.dk.pojo.TPerson;
import com.dk.service.TpersonService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
@org.springframework.stereotype.Service
public class TpersonServiceImpl implements TpersonService {
    @Autowired
    private TPersonMapper tPersonMapper;
    @Autowired
    private PersonRoleMapper personRoleMapper;
    @Override
    public List<TPerson> getInfo() {
        List<TPerson> list = tPersonMapper.selectByExample(null);
        return list;
    }

    @Override
    public boolean addInfo(TPerson tPerson, Integer[] roleId) {
        tPerson.setPwd("1234");
        int i = tPersonMapper.addByUid(tPerson);
        Integer pid = tPerson.getPid();
        int o = personRoleMapper.addAll(roleId,pid);
        return true;
    }

    @Override
    public TPerson getinfoById(Integer id) {
        return tPersonMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean updateInfo(TPerson tPerson, Integer[] roleId) {
        tPersonMapper.updateByPrimaryKeySelective(tPerson);
        PersonRoleExample ure = new PersonRoleExample();
        PersonRoleExample.Criteria criteria = ure.createCriteria();
        criteria.andPersonIdEqualTo(tPerson.getPid());
        personRoleMapper.deleteByExample(ure);
        Integer pid = tPerson.getPid();
        personRoleMapper.addAll(roleId, pid);
        return true;
    }

    @Override
    public boolean deleteId(Integer pid) {
        tPersonMapper.deleteByPrimaryKey(pid);
        PersonRoleExample ure = new PersonRoleExample();
        PersonRoleExample.Criteria criteria = ure.createCriteria();
        criteria.andPersonIdEqualTo(pid);
        personRoleMapper.deleteByExample(ure);
        return true;
    }

    @Override
    public TPerson selectInfo(String pname) {


        return tPersonMapper.selectByUname(pname);
    }
}
